=begin
  This class provides functionality for a Gadget Dashboard application which
  is a web app that allows users to install and use OpenSocial gadgets.
=end
class DashboardController < ApplicationController
  
  # Renders the dashboard view
  # The view requires all gadgets, user installed gadgets, and all categories
  def index
    @gadget_server = 'http://localhost:3002'
    
    #@user = User.find(:first)
    @gadgets = [] #@user.gadgets
    @gadget_json = @gadgets.to_json
    #@all_gadgets = Gadget.find_all_by_published(true)
    @all_gadgets = Gadget.find_all_local(@gadget_server)
    #@categories = Category.find(:all)
  end
  
  
  def p
    @gadgets = Gadget.find_all_by_installed(true)
    @gadget_json = @gadgets.to_json
  end
  
  
  # Adds/Installs a Gadget to the Dashboard
  def add_gadget
    @gadget = Gadget.new('http://localhost:3002/gadget_specs/test/TestGadget.xml')
    gadgets = []
    gadgets.push(@gadget)
    @gadget_json = gadgets.to_json
    render :text=>@gadget_json, :layout=>false
  end
  
  
  # Removes/Uninstalls a Gadget from the Dashboard
  def remove_gadget
    @user = User.find(:first)
    @gadget = Gadget.find_by_container_id(params[:chrome_id])
    #@gadget.update_attribute('installed', false);
    @user.gadgets.delete @gadget
    @gadgets = @user.gadgets
    @gadget_json = @gadgets.to_json
    render :text=>@gadget_json, :layout=>false
  end
  
end
